home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / source / xcode / files.asm < prev    next >
Encoding:
Assembly Source File  |  1993-10-18  |  3.5 KB  |  142 lines

  1. ;----------------------------------------------------------------------------:
  2. ; FILES                                                                      :
  3. ; Code to read files into memory Uses DOS 3.0 + calls                         :
  4. ; (C) John Connors                            1993                             :
  5. ;----------------------------------------------------------------------------:
  6.         
  7.  
  8.         .286
  9.         IDEAL
  10.         MODEL    HUGE,C                        
  11.         
  12. ; equates for int 21h calls
  13.  
  14. DOS_Open        EQU 03dh
  15. DOS_Read        EQU 03fh
  16. DOS_Close        EQU 03eh
  17. DOS_Seek        EQU 042h
  18. DOS_File_Search EQU 04eh
  19. DOS_Next_File    EQU 04fh
  20. DOS_Set_DTA     EQU 01ah
  21.  
  22. ; call an int 21h service
  23.  
  24. MACRO    DOS_Call Service
  25.         MOV     AH,Service
  26.         INT     21h
  27. ENDM
  28.  
  29.         DATASEG
  30.         
  31. STRUC    DOS_DTA                 
  32.                     db 21 dup (0)                ; DTA, used by DOS as
  33.     file_attributes db 0                        ; scratchpad for file info
  34.     File_Time        dw 0
  35.     File_Date        dw 0
  36.     File_Size        dd 0
  37.     File_Name        db 13 dup (0)
  38.  
  39. ENDS    DOS_DTA
  40.  
  41. DTA DOS_DTA     <>
  42.                     db 128 dup (0)                ; for safety
  43.         CODESEG
  44.  
  45. ;----------------------------------------------------------------------------:
  46. ;            CODE SEGMENT                                                     :
  47. ;----------------------------------------------------------------------------:
  48.  
  49.         PUBLIC get_file
  50.         PUBLIC get_file_size
  51.  
  52. ;----------------------------------------------------------------------------:
  53. ; Get file - suck file into memory.                                          :
  54. ; Enter with EAX pointing to entry in file name list                         :
  55. ; Exit    with ECX is size of file in bytes or 0 for an error                  :
  56. ;        file is read into File_Segment at offset File_Data                     :
  57. ;----------------------------------------------------------------------------:
  58.  
  59. PROC    get_file    
  60.         USES    ES,DS,SI,DI
  61.         ARG     file_name:DATAPTR,file_buffer:DATAPTR
  62.  
  63.         LDS     SI,[file_name]                    ; DS:SI points to file name list
  64.         PUSH    DS
  65.         PUSH    SI
  66.         CALL    PROC get_file_size                ; get the size of the file
  67.         ADD     SP,4
  68.         
  69.         OR        AX,AX
  70.         JZ        SHORT @@Error                    ; something went wrong
  71.         PUSH    AX                                ; save file size
  72.         
  73.         MOV     DX,SI                            ; point to file name
  74.         MOV     AL,11000000b                    ; file open code
  75.  
  76.         DOS_Call    DOS_Open                    ; open file 
  77.  
  78.         POP     CX
  79.         JC        SHORT @@Error                    ; something went wrong
  80.         
  81.         MOV     BX,AX                            ; BX = file handle
  82.         LDS     DX,[file_buffer]
  83.  
  84.         DOS_Call    DOS_Read        
  85.  
  86.         JC        SHORT @@Error                    ; ok? no - oh, balls..
  87.         MOV     CX,AX                            ; EAX = bytes read
  88.  
  89.         DOS_Call    DOS_Close
  90.  
  91.         JC        SHORT @@Error                    ; oh, dammn..
  92.         JMP     SHORT @@get_exit                ; okay
  93. @@Error:    
  94.         XOR     CX,CX 
  95. @@get_exit: 
  96.         MOV     AX,CX
  97.         
  98.         RET
  99. ENDP    get_file
  100.         
  101. ;----------------------------------------------------------------------------:
  102. ; File Size - return file size                                                 :
  103. ; Enter with DS:SI pointing to ASCIIZ file name                              :
  104. ; Exit with EAX holding size of file in bytes                                 :
  105. ;----------------------------------------------------------------------------:
  106.  
  107. PROC get_file_size  
  108.         USES    BX,DX,CX,SI,DI,DS,ES
  109.         ARG     File_Name:DATAPTR
  110.     
  111.         LES     SI,[File_Name]
  112.         PUSH    SEG DTA
  113.         POP     DS
  114.         MOV     DX,OFFSET DTA                    ; DS:DX = Dta address
  115.         DOS_Call    DOS_Set_DTA                 ; set DTA to DS:DX
  116.         
  117.         PUSH    ES                                ; set DS to filename segment
  118.         POP     DS
  119.         
  120.         MOV     DX,SI                            ; DS:DX == file name
  121.         XOR     CX,CX                            ; CX = search attributes 
  122.         DOS_Call    DOS_File_Search             ; look for file
  123.         JC        SHORT @@Error
  124.  
  125.         PUSH    SEG DTA                         ; get the length from DTA
  126.         POP     DS
  127.         ASSUME    DS:SEG DTA  
  128.         MOV     SI,OFFSET DTA
  129.         MOV     AX,[WORD (DOS_DTA PTR SI).File_Size]
  130.         JMP     SHORT @@fil_exit
  131.         
  132. @@Error:    
  133.         XOR     AX,AX
  134.         
  135. @@fil_exit:
  136.         
  137.         RET     
  138.  
  139. ENDP    get_file_size
  140.  
  141.         END
  142.